package com.app.service;

import com.alibaba.fastjson.JSON;
import com.app.dao.mysql.YjDataMapper;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.kzgs.KzgsResultJson;
import com.app.domain.kzgs.Record;
import com.app.domain.kzgs.RecordDetails;
import com.app.utils.HttpClients;
import com.app.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class KzgsService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    YjDataMapper yjDataMapper;


    public Record getRecord(List<YjData> yjData, UploadParameter uploadParameter, String deviceNo) {
        Record record = new Record();
        record.setDeviceNo(deviceNo);
        record.setContractingUnit(uploadParameter.getSgdw());
        record.setSupervisionUnit(uploadParameter.getJldw());
        record.setProject(uploadParameter.getGcmc());
        record.setEngineering(uploadParameter.getGcmc());
        record.setBridgeNo(yjData.get(0).getBridge_code());
        record.setRecordTime(yjData.get(0).getMudjack_date() + " " + yjData.get(0).getBegin_time());
        List<RecordDetails> details = getDetails(yjData, uploadParameter);
        record.setDetails(details);
        return record;
    }

    private List<RecordDetails> getDetails(List<YjData> yjDataList, UploadParameter uploadParameter) {
        List<RecordDetails> detailsList = new ArrayList<>();
        int count = 1;
        for (YjData yjData : yjDataList) {
            RecordDetails details = new RecordDetails();
            details.setRecordTime(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            details.setHoleLocation(yjData.getDuct_code());
            details.setTensionControl("0.6");
            details.setBeginTime(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            details.setEndTime(yjData.getMudjack_date() + " " + yjData.getEnd_time());
            details.setPressure( yjData.getMudjack_pressure());
            details.setBackPressure( yjData.getSlurry_pressure());
            details.setLoadHoldingTime(Float.parseFloat(yjData.getPressurize_time()));
            details.setMixTime(Float.parseFloat(uploadParameter.getStirtime()));
            details.setWaterBinderRatio( yjData.getWater_binder_ratio());
            details.setMixProportion_SN(100);
            details.setMixProportion_YJJ(0);
            details.setMixProportion_S(28);
            details.setHoleSequence(String.valueOf(count));
            details.setHoleLength(Integer.parseInt(uploadParameter.getHolelength()));
            details.setGroutingHolePosition("常规");
            details.setDrainPosition("常规");
            details.setDesignPressure(0.75f);
            details.setDesignBackPressure(0.75f);
            details.setDesignPressure(0.75f);
            details.setDesignBackPressure(0.75f);
            details.setOverflowCondition("正常");
            details.setPassingSituation("正常");
            details.setSlurryDischargeSituation("正常");
            details.setVolume( yjData.getPractical_volume());
            detailsList.add(details);
            count++;
        }
        return detailsList;
    }

    public KzgsResultJson sendMessage(String serviceUrl, String json) {
        String result = HttpClients.requestPost(serviceUrl, json);
        KzgsResultJson resultJson = (KzgsResultJson) JSON.parseObject(StringUtil.replaceBlank(result), KzgsResultJson.class);
        return resultJson;
    }


}
